package com.yyge.vo;


/**
 * 对结果进行包装的类
 */
public class R{
    // 代表状态码
    private Integer code;
    // 代表消息
    private String message;
    // 代表包装的数据
    private Object data;

    private static final Integer SUCCESS = ResultCode.OK.getCode();
    private static final Integer FAILURE = ResultCode.FAILURE.getCode();

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    /**
     * 通用成功的方法
     * @return
     */
    public static R ok(){
        return  restResult(SUCCESS, ResultCode.OK.getMessage(), null);
    }
    public static R ok(String message){
        return restResult(SUCCESS, message, null);
    }
    public static R ok(Object data){
        return restResult(SUCCESS, null, data);
    }
    public static R ok(String message,Object data){
        return restResult(SUCCESS, message, data);
    }

    /**
     * 通用失败的方法
     * @return
     */
    public static R fail(){
        return  restResult(FAILURE, "操作失败", null);
    }
    public static R fail(String message){
        return restResult(FAILURE, message, null);
    }
    public static R fail(Object data){
        return restResult(FAILURE, null, data);
    }
    public static R fail(String message,Object data){
        return restResult(FAILURE, message, data);
    }

    /**
     * 根据枚举得到失败的R对象
     * @param resultCode
     * @return
     */
    public static R fail(ResultCode resultCode) {
        return restResult(resultCode.getCode(), resultCode.getMessage(), null);
    }

    public static R restResult(int code, String message, Object data) {
        R r = new R();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

}